# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: db_handler.py
处理数据
Author: kij
Create Date: 2021/2/23
-------------------------------------------------
"""
import os
import pickle

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def save_data(data):

	folder_path = os.path.join(BASE_DIR, "files", data.__class__.__name__)

	if not os.path.exists(folder_path):
		os.mkdir(folder_path)
	with open(os.path.join(folder_path,data.user_name), mode='wb') as file_obj:
		pickle.dump(data, file_obj)


def get_data(cls, file_name):
	folder_path = os.path.join(BASE_DIR, "files", cls.__name__)
	if not os.path.join(folder_path, file_name):
		return False, "文件不存在"
	file_path = os.path.join(folder_path, file_name)
	if os.path.exists(file_path):
		with open(file_path, mode='rb') as file_obj:
			data = pickle.load(file_obj)
			return data



